<?php

require_once 'VO/VOPicture.php';
require("config.inc.php"); 
/****************************************
 *    Class PictureHandler
 *    Handles request related with pictures like
 *    getMyPictures
 *    deletePictures
 ****************************************/
 class PictureHandler
 {
 	/*
 	 * 	 get from the database all of the pictures of a user, from the specified
 	 *  trip and place. 
 	 */
 	
 	public $link_id;
 	
 	public function __construct()
 	{
 	 	//connect to the database.
		//we could have used an abstracting layer for connecting to the database.
		//for the sake of simplicity, I choose not to.
		$this->link_id = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
		mysql_select_db(DB_DATABASE);
 	 	
 	}
 	public function getMyPictures($userId,$tripId, $placeId)
 	{

 		/*
	 * Save the picture in the database and
	 * get the next picture index from the database
	 */ 
	$DATABASE_SERVER = "localhost";
	$DATABASE_PASSWORD = "";
	$DATABASE_USERNAME = "root";
	$DATABASE_NAME = "tours";
//	$DATABASE_SERVER = "91.121.56.68";
//	$DATABASE_PASSWORD = "vub_wis";
//	$DATABASE_USERNAME = "wis_developer";
//	$DATABASE_NAME = "tours";
	
	$mysql = mysql_connect($DATABASE_SERVER, $DATABASE_USERNAME, $DATABASE_PASSWORD);
	mysql_select_db($DATABASE_NAME);
	
	
	$query = "SELECT * FROM pictures
			  WHERE (userId=".$userId." and tripId=".$tripId." and placeId=".$placeId.")";
	$pictures = mysql_query($query);
	
	  /************** places list *********************/
      $myPictures = array();
	  while ($row = mysql_fetch_object($pictures, "VOPicture")) 
	  {
			$myPictures[] = $row;
			//echo $row->place_id;
	  }
	  return $myPictures;     
	
 	}
 	
 	/**
 	 *   Deletes the pictures of a user from a given trip and place 
 	 * @param $userId - the user identifier
 	 * @param $tripId - the trip identifier
 	 * @param $placeId - the place identifier
 	 * @param $pictures - the array of picture identifiers what should be deleted
 	 */
 	public function deletePictures ($userId,$tripId, $placeId, $pictures)
 	{
 		return;
 	}
 	
 	public function savePicture ($userId,$tripId, $placeId, $picture_name)
 	{
 			$sql_insert_picture = "INSERT INTO pictures (title,tripId, placeId,userId) VALUES ('".$picture_name.
			"',".$tripId.",".
			$placeId.",".
			$userId.")";
			echo $sql_insert_picture;		
			
			$result_insert_picture = @mysql_query($sql_insert_picture,$this->link_id);
			echo $result_insert_picture; 
			$new_picture_id = mysql_insert_id();
			echo 'plis'.$new_picture_id;			
			//$comment_obj->id = $new_comment_id;
 		
 		return(1);
 	}
 	
 }
?>